package edu.tongji.jiaowu.webapp.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import java.util.Iterator;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.util.JsonDataSet;
import com.tongji.quickCRUDQ.util.RequestUtil;

import edu.tongji.jiaowu.service.ParameterManager;
import edu.tongji.jiaowu.service.VcodeManager;

public class ParameterController extends MultiActionController {
	private ParameterManager parameterManager;
	private VcodeManager vcodeManager;

	public ModelAndView parameter(HttpServletRequest request,HttpServletResponse response){
		String parameterType=request.getParameter("parameterType");
		Map model=new HashMap();
		String[] params=null;
		if("timetable".equalsIgnoreCase(parameterType)){
			params=new String[]{"TIMETABLE_YEAR","TIMETABLE_SEMESTER"};
			CommonBean schoolYear=vcodeManager.findCodes("SCHOOL_YEAR");
			model.put("schoolYear", JsonDataSet.listToJson(schoolYear.getData()));
			CommonBean schoolSemester=vcodeManager.findCodes("SCHOOL_SEMESTER");
			model.put("schoolSemester", JsonDataSet.listToJson(schoolSemester.getData()));
		}
		if("elective".equalsIgnoreCase(parameterType)){
			params=new String[]{"ELECTIVE_YEAR","ELECTIVE_SEMESTER"};
			CommonBean schoolYear=vcodeManager.findCodes("SCHOOL_YEAR");
			model.put("schoolYear", JsonDataSet.listToJson(schoolYear.getData()));
			CommonBean schoolSemester=vcodeManager.findCodes("SCHOOL_SEMESTER");
			model.put("schoolSemester", JsonDataSet.listToJson(schoolSemester.getData()));
		}
		if("reexamination".equalsIgnoreCase(parameterType)){
			params=new String[]{"REEXAMINATION_YEAR","REEXAMINATION_SEMESTER"};
			CommonBean schoolYear=vcodeManager.findCodes("SCHOOL_YEAR");
			model.put("schoolYear", JsonDataSet.listToJson(schoolYear.getData()));
			CommonBean schoolSemester=vcodeManager.findCodes("SCHOOL_SEMESTER");
			model.put("schoolSemester", JsonDataSet.listToJson(schoolSemester.getData()));
		}
		if("restudy".equalsIgnoreCase(parameterType)){
			params=new String[]{"RESTUDY_YEAR","RESTUDY_SEMESTER"};
			CommonBean schoolYear=vcodeManager.findCodes("SCHOOL_YEAR");
			model.put("schoolYear", JsonDataSet.listToJson(schoolYear.getData()));
			CommonBean schoolSemester=vcodeManager.findCodes("SCHOOL_SEMESTER");
			model.put("schoolSemester", JsonDataSet.listToJson(schoolSemester.getData()));
		}
		CommonBean parameter=parameterManager.findParameters(params);
		String json=JsonDataSet.listToJson(parameter.getData());
		model.put("json", json);
		return new ModelAndView("/parameter/"+parameterType,"model",model);
	}
	
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response){
		Map parameter=new HashMap();
		Enumeration names=request.getParameterNames();
		while (names.hasMoreElements()) {
			String name= (String) names.nextElement();
			if ("ajax_extraParams".equals(name)) {
				continue;
			}
			String value=request.getParameter(name);
			parameter.put(name, value);
		}
		parameterManager.updateParameter(parameter);
		RequestUtil.write(response, "{success:true,msg:'保存成功'}");
		return null;
	}
	
	public ParameterManager getParameterManager() {
		return parameterManager;
	}

	public void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}

	public VcodeManager getVcodeManager() {
		return vcodeManager;
	}

	public void setVcodeManager(VcodeManager vcodeManager) {
		this.vcodeManager = vcodeManager;
	}

}
